<?php
namespace Home\Controller;

use Think\Controller;

class IndexController extends Controller
{
    public function index()
    {
        $userInfo = GetUserInfo();
        if ($userInfo['status']) {
            $resultArr = array(
                'firstBtnUrl' => U('Home/Index/info'),
                'firstBtnText' => '个人信息',
                'secondBtnUrl' => U('Admin/Index/index'),
                'secondBtnText' => '后台管理',
                'userInfoUrl' => U('Home/Index/info'),
                'userAdminUrl' => U('Admin/Index/index'),
                'userLogoutUrl' => U('Admin/User/logout'),
                'userName' => $userInfo['info']['username']
            );
        } else {
            $resultArr = array(
                'firstBtnUrl' => U('Admin/User/login'),
                'firstBtnText' => '登录',
                'secondBtnUrl' => U('Admin/User/register'),
                'secondBtnText' => '注册',
                'navStyle' => 'display: none'
            );
        }

        $this->assign('result', $resultArr);
        $this->display();
    }

    public function userinfo()
    {

        $userInfo = GetUserInfo();
        if ($userInfo['status']) {
            $Privilege = array(
                'userType' => array(
                    '-1' => '系统管理员',
                    '0' => '未登录',
                    '1' => '注册用户',
                    '10' => '超级管理员'

                ),
                'userPrivilege' => array(
                    '-1' => '系统权限',
                    '0' => '无权限',
                    '1' => '浏览自己信息,修改自己信息(提交确认)',
                    '10' => '查看所有用户,修改所有用户权限'
                )
            );
            $resultArr = array(
                'userName' => $userInfo['info']['username'],
                'userType' => $Privilege['userType'][(string)$userInfo['info']['userprivilege']],
                'userPrivilege' => $Privilege['userPrivilege'][(string)$userInfo['info']['userprivilege']]
            );
            $this->assign('result', $resultArr);
            $this->display();
        } else {
            header('Location: ' . U('Home/Index/index'));
        }

    }

    public function loginlog()
    {
        $userInfo = GetUserInfo();
        if ($userInfo['status']) {
            $Privilege = array(
                'userType' => array(
                    '-1' => '系统管理员',
                    '0' => '未登录',
                    '1' => '注册用户',
                    '10' => '超级管理员'

                ),
                'userPrivilege' => array(
                    '-1' => '系统权限',
                    '0' => '无权限',
                    '1' => '浏览自己信息,修改自己信息(提交确认)',
                    '10' => '查看所有用户,修改所有用户权限'
                )
            );
            $resultArr = array(
                'userName' => $userInfo['info']['username'],
                'userType' => $Privilege['userType'][(string)$userInfo['info']['userprivilege']],
                'userPrivilege' => $Privilege['userPrivilege'][(string)$userInfo['info']['userprivilege']]
            );
            $this->assign('result', $resultArr);
            $this->display();
        } else {
            header('Location: ' . U('Home/Index/index'));
        }
    }

    public function ajaxloginlog()
    {
        $userInfo = GetUserInfo();
        $postData = I('post.index', false);
        if ($userInfo['status']) {
            if ($postData !== false) {
                $usermodel = new \Home\Model\UserModel();
                $result = $usermodel->getLog($userInfo['info']['uid'], $postData);
                echo json_encode($result);
            } else {
                echo json_encode(array('status'=>false, 'info'=>I('post.')));
            }
        } else {
            header('Location: ' . U('Home/Index/index'));
        }
    }

    public function dolog()
    {
        $this->display();
    }

    public function info()
    {
        $userInfo = GetUserInfo();
        if ($userInfo['status']) {
            $this->display();
        } else {
            header('Location: ' . U('Home/Index/index'));
        }
    }
}